import aepApiRequest, {cozeApiRequest} from '../utils/aepApiRequest';
import language from '../models/language';
/**
 * 获取手机验证码
 * @param {object} parmas
 * @param {string} parmas.mobile
 * @param {number} parmas.type
 */
export async function getMobileCode(parmas: { mobile: string, type: number }) {
  const languageKey = language.key;
  return aepApiRequest(
    'app/user/mobileCode',
    {
      method: 'POST',
      body: { ...parmas, lang: languageKey },
    },
    false
  );
}

interface IRegisterParams {
  account: string;
  code: string;
  home?: boolean;
  homeName?: string;
  password: string;
  refresh?: boolean;
}

export async function registerByMail(params: IRegisterParams) {
  return aepApiRequest(
    'app/smart_home/register/pwd',
    {
      method: "POST",
      body: { ...params, home: true, refresh: false }
    },
    false
  )
}

export async function loginByPwd(parmas: {
  account: string;
  password: string;
}) {
  const languageKey = language.key;
  return aepApiRequest(
    'app/smart_home/login/pwd',
    {
      method: 'POST',
      body: { ...parmas, lang: languageKey, refreshToken: true },
    },
    false
  );
}

interface ResetPwdParams {
  code: string
  email: string
  password: string
}

export async function resetPwd(params: ResetPwdParams) {
  return aepApiRequest(
    'app/smart_home/reset_pwd',
    {
      method: "POST",
      body: { ...params, chkWeakPwd: 1 }
    },
    false
  )
}

export async function getMailCode(parmas: { mail: string, type: number }) {
  const languageKey = language.key;
  return aepApiRequest(
    'app/user/mailCode',
    {
      method: 'POST',
      body: { ...parmas, lang: languageKey },
    },
    false
  );
}

export async function loginByMail(parmas: {
  mail: string;
  code: string;
  homeName?: string;
}) {
  const languageKey = language.key;
  return aepApiRequest(
    'app/user/loginByMail',
    {
      method: 'POST',
      body: { ...parmas, lang: languageKey },
    },
    false
  );
}

export async function loginByMobile(parmas: {
  mobile: string;
  code: string;
  homeName?: string;
}) {
  const languageKey = language.key;
  return aepApiRequest(
    'app/user/loginByMobile',
    {
      method: 'POST',
      body: { ...parmas, lang: languageKey },
    },
    false
  );
}

export async function feedback(parmas: { phone: string; content: string }) {
  return aepApiRequest('app/feedback/create', {
    method: 'POST',
    body: parmas,
  });
}

export async function wechatPhoneLogin({ code, encryptedData, iv }) {
  return cozeApiRequest(`app/applets/thirdLoginUser?code=${code}`, {
    method: 'POST',
    body: { encryptedData, iv },
  });
}

export async function wechatLoginByCode({ code }) {
  const res = await cozeApiRequest(`v3/users/oauth/wechat?code=${code}`, {
    method: 'POST',
    body: {},
  });
  console.log('data.data', res)
  if (res.success) {
    return {
      data: {
        token: res.data.data.token.access_token,
        uid: res.data.data.user.id,
        openid: res.data.data.user.openid,
        createdAt: res.data.data.token.created_at,
        expiredAt: res.data.data.token.expires_in,
      },
      success: true
    }
  }
  return data;
}
